using Jupiter.Infrastructure.Permission.Attributes;
using Jupiter.Infrastructure.Responses;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Wips.AppService.Authorities.Positions;
using Wips.AppService.Authorities.Positions.Requests;
using Wips.AppService.Authorities.Positions.Responses;
using Wips.AppService.CommonViewModels;

namespace Wips.MainServer.Controllers.Authorities
{
    /// <summary>
    /// 职位
    /// </summary>
    public class PositionController : CustomeBaseController
    {
        private readonly IPositionService _service;
        private readonly IMediator _mediator;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="service"></param>
        /// <param name="mediator"></param>
        public PositionController(
            IPositionService service,
            IMediator mediator)
        {
            _service = service;
            _mediator = mediator;
        }

        #region 基础接口

        /// <summary>
        /// 读取分页列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [Permission]
        public async Task<PageResult<GetPositionPagesResponse>> GetPagesAsync(
            [FromBody] GetPositionPagesRequest request)
        {
            return await _service.GetPagesAsync(request);
        }

        /// <summary>
        /// 根据ID读取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Permission]
        public async Task<GetPositionByIdResponse> GetAsync([FromQuery] string id)
        {
            return await _service.GetByIdAsync(id);
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [Permission]
        public async Task<string> PostAsync([FromBody] CreatePositionRequest request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        [Permission]
        public async Task<string> PutAsync([FromBody] UpdatePositionRequest request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpDelete]
        [Permission]
        public async Task<string> DeleteAsync([FromBody] DeletePositionRequest request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 状态变更
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        [Permission]
        public async Task<string> StatusChangeAsync([FromBody] PositionStatusChangeRequest request)
        {
            return await _mediator.Send(request);
        }

        #endregion

        #region 扩展接口

        /// <summary>
        /// 读取树形结构的职位数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Permission]
        public async Task<List< TreeSelectViewModel>> GetTreeSelectDataAsync()
        {
            return await _service.GetTreeSelectDataAsync();
        }

        /// <summary>
        /// 根据角色Id读取职位Id列表
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <returns></returns>
        [HttpGet]
        [Permission]
        public async Task<List<string>> GetIdsByRoleIdAsync([FromQuery] string roleId)
        {
            return await _service.GetIdsByRoleIdAsync(roleId);
        }

        /// <summary>
        /// 为职位分配角色
        /// </summary>
        /// <param name="request">请求主体</param>
        /// <returns></returns>
        [HttpPost]
        [Permission]
        public async Task<string> AssignRolesAsync([FromBody] AssignRolesForPositionRequest request)
        {
            return await _mediator.Send(request);
        }

        #endregion
    }
}